package com.moyu.user.controller.role;

import com.moyu.common.core.controller.BaseController;
import com.moyu.common.core.domain.R;
import com.moyu.common.core.domain.TableDataInfo;
import com.moyu.user.domain.role.dto.RoleQueryDTO;
import com.moyu.user.domain.role.dto.RoleSaveDTO;
import com.moyu.user.domain.role.vo.RoleVO;
import com.moyu.user.service.role.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制层
 *
 * @Author 赵彦博
 * @Date 2024/12/27 15:33
 */
@RestController
@RequestMapping("/role")
@Tag(name = "角色接口")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/add")
    @Operation(summary = "新增角色", description = "根据提供的信息角色")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "4002", description = "角色已存在")
    public R<Void> add(@RequestBody RoleSaveDTO roleSaveDTO) {
        return toR(roleService.add(roleSaveDTO));
    }

    @PostMapping("/addRoleWithUser/{id}/roles")
    @Operation(summary = "为用户新增角色", description = "根据提供的信息为用户新增角色")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "5002", description = "角色已存在")
    public R<Void> addRoleWithUser(@PathVariable Long id, @RequestBody List<RoleVO> roles) {
        return toR(roleService.addRoleWithUser(id, roles));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色", description = "通过角色id删除角色")
    @Parameters(value = {
            @Parameter(name = "roleId", in = ParameterIn.PATH, description = "角色ID")
    })
    @ApiResponse(responseCode = "1000", description = "成功删除角色")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "4001", description = "角色不存在")
    public R<Void> delete(@PathVariable Long id) {
        return toR(roleService.delete(id));
    }

    @GetMapping("/list")
    @Operation(summary = "查询角色列表", description = "根据提供的信息查找角色列表")
    @ApiResponse(responseCode = "1000", description = "成功查询角色列表")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public TableDataInfo list(RoleQueryDTO roleQueryDTO) {
        return getTableDataInfo(roleService.list(roleQueryDTO));
    }

    @GetMapping("/listByUserId")
    @Operation(summary = "根据用户ID查询角色列表", description = "根据提供的信息查找角色列表")
    @ApiResponse(responseCode = "1000", description = "成功查询角色列表")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public TableDataInfo listByUserId(RoleQueryDTO roleQueryDTO) {
        return getTableDataInfo(roleService.listByUserId(roleQueryDTO));
    }

    @GetMapping("/listByUserIdAll")
    @Operation(summary = "根据用户ID查询所有角色", description = "根据提供的信息查找角色列表")
    @ApiResponse(responseCode = "1000", description = "成功查询角色列表")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public List<RoleVO> listByUserIdAll(Long id) {
        return roleService.listByUserIdAll(id);
    }

}
